package lium.lbs.lbsim.adapter

import android.widget.TextView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.buz.hjcdriver.bean.FriendsModel
import com.buz.hjcdriver.bean.FriendsResultModel
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.qmuiteam.qmui.widget.QMUIRadiusImageView
import lium.lbs.lbsim.App
import lium.lbs.lbsim.R
import lium.lbs.lbsim.jgchat.activity.MainActivity
import lium.lbs.lbsim.jgchat.utils.photovideo.takevideo.utils.LogUtils
import lium.lbs.lbsim.utils.PinnedHeaderItemDecoration

/**
 * 好友 adapter
 */
class FriendListAdapter
    (data: ArrayList<FriendsResultModel>) : BaseMultiItemQuickAdapter<FriendsResultModel, BaseViewHolder>(data),
    PinnedHeaderItemDecoration.PinnedHeaderAdapter {

    override fun isPinnedViewType(viewType: Int): Boolean {
        return viewType == FriendsModel.ITEM_TYPE_SETION
    }

    var mainActivity: MainActivity? = null

    init {
        mainActivity = mContext as MainActivity?
        addItemType(FriendsModel.ITEM_TYPE_DATA, R.layout.item_contact_list_context)
        addItemType(FriendsModel.ITEM_TYPE_SETION, R.layout.item_list_setion)
    }

    override fun convert(helper: BaseViewHolder?, item: FriendsResultModel?) {
        helper?.let {
            item?.let {
                when (helper.itemViewType) {
                    FriendsModel.ITEM_TYPE_DATA -> {
                        initData(helper, item)
                    }
                    FriendsModel.ITEM_TYPE_SETION -> {
                        helper.getView<TextView>(R.id.item_setion).text = item.letters + ""
                    }
                }
            }
        }
    }

    private fun initData(helper: BaseViewHolder, item: FriendsResultModel) {
        helper.getView<TextView>(R.id.contacts_name).text = if (item?.remark == null) {
            item?.nickName
        } else {
            item?.remark
        }
        val options = RequestOptions()
        Glide.with(App.getContext())
            .load(item.headImage)
            .apply(options.placeholder(R.drawable.icon_defalut_head))
            .into(helper.getView<QMUIRadiusImageView>(R.id.contacts_myhead))

        LogUtils.e("body--" + item.headImage)
    }

}